﻿using System.Text;

namespace TPXSDK.NET.Mission.Instruction;

internal class InstAltKind : Tokenizer
{
    public InstAltKind(TokenizerAdmin admin)
    {
        Admin = admin;
        KeyName = "altkind";
        KeyValue = (byte)FlyCmdId.CMD_ALTKIND;
        Usage = "altkind [def|abs|rel|agl]";
        Description = @"Set Altitude kind flag";
    }

    /// <inheritdoc />
    /// <summary>
    /// 词元分析器处理过程
    /// </summary>
    public override bool Process()
    {
        var flyCmp = Admin?.Compiler; if (null == flyCmp) return false;
        var tokens = flyCmp.GetLineTokens();

        if (null == tokens || tokens.Count != 2)
        {
            flyCmp.ErrorMsg = $"{KeyName} Invalid Usage";
            return false;
        }

        var cmd = new FlyCmdItem
        {
            Id = (byte)KeyValue,
            LineNum = tokens[0].Row
        };

        if (tokens[1].Name == "def")
            cmd.P1 = 0;
        else if (tokens[1].Name == "abs")
            cmd.P1 = 1;
        else if (tokens[1].Name == "rel")
            cmd.P1 = 2;
        else if (tokens[1].Name == "agl")
            cmd.P1 = 3;
        else
        {
            flyCmp.ErrorMsg = $"{KeyName} Invalid param";
            return false;
        }

        flyCmp.CmdList.Add(cmd);
        return true;
    }

    public override void Reverse(FlyCmdItem cmd, StringBuilder sb)
    {
        if (0 == cmd.P1)
            sb.AppendLine(@"altkind def");
        else if (1 == cmd.P1)
            sb.AppendLine(@"altkind abs");
        else if (2 == cmd.P1)
            sb.AppendLine(@"altkind rel");
        else if (3 == cmd.P1)
            sb.AppendLine(@"altkind agl");
        else
            sb.AppendLine(@"altkind ???");
    }
}
